package javase.javase01.decorator;

/**
 * @author: Gan
 * @date 2021/4/17 0017 21:51
 * @description:装饰设计模式：不改变原有类的基础上去扩展新的功能
 **/
public class Demo {
    public static void main(String[] args) {
        //原有基础类
        MilkTea milkTea = new MilkTea();
        milkTea.add();
        System.out.println("=============");
        //装饰类A
        DecoratorA decoratorA = new DecoratorA();
        decoratorA.setBeverage(milkTea);
        decoratorA.add();
        System.out.println("=============");
        //装饰类B
        DecoratorB decoratorB = new DecoratorB();
        decoratorB.setBeverage(decoratorA);
        decoratorB.add();
        System.out.println("==============");
        //装饰类c
        DecoratorC decoratorC = new DecoratorC();
        decoratorC.setBeverage(decoratorB);
        decoratorC.add();
    }
}

/*表示饮料 基类*/
abstract class Beverage {
    //加小料
    abstract void add();
}

class MilkTea extends Beverage {

    @Override
    void add() {
        System.out.println("奶茶加冰。");
    }
}

/*装饰器类*/
class Decorator extends Beverage {
    public Beverage beverage;

    public void setBeverage(Beverage beverage) {
        this.beverage = beverage;
    }

    @Override
    void add() {
        beverage.add();
    }
}

//装饰类A
class DecoratorA extends Decorator {

    @Override
    void add() {
        super.add();
        addZz();
    }

    private void addZz() {
        System.out.println("再加点珍珠。");
    }
}

//装饰类B
class DecoratorB extends Decorator {
    @Override
    void add() {
        super.add();
        addYg();
    }

    private void addYg() {
        System.out.println("再加点椰果。");
    }
}

//装饰类C
class DecoratorC extends Decorator {
    @Override
    void add() {
        super.add();
        addSx();
    }

    private void addSx() {
        System.out.println("再加点烧仙草！");
    }
}